<style lang="scss" scoped>

</style>

<template>
    <div class="infobody">
         <top-bar ref="topbar" @partToggle="partToggle" @toggleEditStatus="toggleEditStatus" @saveItem="saveItem" @saveTemp="saveTemp" @clickNextStep="clickNextStep" ></top-bar>
        <div class="content">
           <loading v-model="spinShow"></loading>
          <p class="form-title">{{formTitle}}</p>
          <div id="part1" v-show="partShow"> 
              <Form :model="rareSampleModel" ref="rareSampleModelOne" :rules="ruleFormOne">
                <div class="title">
                    个人信息
                    <span class="title-right" @click="personToggle=!personToggle" v-show="personToggle"><Icon type="minus-round"></Icon></span>
                    <span class="title-right" @click="personToggle=!personToggle" v-show="!personToggle"><Icon type="plus-round"></Icon></span>                   
                </div>
                <div class="info" v-show="personToggle">
                    <FormItem label="userId" hidden>
                        <Input v-model="rareSampleModel.tRareSampleEntity.userId" />
                    </FormItem> 
                    <FormItem label="姓名" prop="tRareSampleEntity.name">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.name" placeholder="请输入姓名" />
                    </FormItem>
                    <FormItem label="性别" prop="tRareSampleEntity.sex">
                        <Select :disabled="status" v-model="rareSampleModel.tRareSampleEntity.sex" placeholder="请选择性别">
                            <Option value="男">男</Option>
                            <Option value="女">女</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="出生日期" prop="tRareSampleEntity.birthday">
                      <!-- MUI方式 -->      
                      <div class="ivu-input-wrapper ivu-input-type">
                          <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('birthday')">
                            <span v-if="rareSampleModel.tRareSampleEntity.birthday">{{rareSampleModel.tRareSampleEntity.birthday}}</span>
                              <span v-else   style="color:#bbbec4;">请选择出生日期</span>
                          </div>
                      </div>    

                    </FormItem>
                    <FormItem label="样本编号" prop="tRareSampleEntity.sampleNum">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.sampleNum" placeholder="请输入样本编号" />
                    </FormItem>
                    <FormItem label="申请医院">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.hospital" placeholder="请输入医院名称" />
                    </FormItem>
                    <FormItem label="申请医生">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.doctor" placeholder="请输入医生姓名" />
                    </FormItem>
                    <FormItem label="报告邮寄信息" prop="tRareSampleEntity.address">
                        <!-- <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.address" placeholder="请输入报告邮寄信息" /> -->
                        <AutoComplete :disabled="status" v-model="rareSampleModel.tRareSampleEntity.address" :data="addressList" :filter-method="filterAddress"   placeholder="请输入报告邮寄信息" ></AutoComplete>
                    </FormItem>
                    <FormItem label="怀疑疾病" prop="tRareSampleEntity.susDisease">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.susDisease" placeholder="请输入怀疑疾病" />
                    </FormItem>
                    <FormItem label="临床关键词" prop="tRareSampleEntity.cliKeyWord">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.cliKeyWord" type="textarea"  placeholder="请输入临床关键词" /> 
                    </FormItem>
                    <FormItem label="采样日期" prop="tRareSampleEntity.samplingDate">   
                      <div class="ivu-input-wrapper ivu-input-type">
                        <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('samplingDate')">
                            <span v-if="rareSampleModel.tRareSampleEntity.samplingDate">{{rareSampleModel.tRareSampleEntity.samplingDate}}</span>
                            <span v-else   style="color:#bbbec4;">请选择采样日期</span>
                        </div>
                      </div>   
                    </FormItem> 
                    <FormItem label="首次发病日期" prop="tRareSampleEntity.firstDate">   
                      <div class="ivu-input-wrapper ivu-input-type">
                        <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('firstDate')">
                          <span v-if="rareSampleModel.tRareSampleEntity.firstDate">{{rareSampleModel.tRareSampleEntity.firstDate}}</span>
                          <span v-else   style="color:#bbbec4;">请选择首次发病日期</span>
                        </div>
                      </div>   
                    </FormItem>                     
                    <FormItem label="样本类型" prop="tRareSampleEntity.samplingTypeArray" >            
                        <CheckboxGroup v-model="rareSampleModel.tRareSampleEntity.samplingTypeArray">
                          <Checkbox label="石蜡包埋组织/切片" :disabled="status"></Checkbox>
                          <Checkbox label="手术/穿刺组织" :disabled="status"></Checkbox>
                          <Checkbox label="胸/腹水" :disabled="status"></Checkbox>
                          <Checkbox label="EDTA抗凝" :disabled="status"></Checkbox>
                          <Checkbox label="Streck管" :disabled="status"></Checkbox>
                          <Checkbox label="其他" :disabled="status"></Checkbox>
                        </CheckboxGroup>
                    </FormItem>
                    <FormItem  v-show="rareSampleModel.tRareSampleEntity.samplingTypeArray?rareSampleModel.tRareSampleEntity.samplingTypeArray.indexOf('其他')!=-1:false">   
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.itsSamplingType"  placeholder="请输入样本类型" />
                    </FormItem>   
                    <FormItem label="补送样本">
                      <Select :disabled="status" v-model="rareSampleModel.tRareSampleEntity.replacesample">
                            <Option value="是">是</Option>
                            <Option value="否">否</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="科研样本">
                       <Select :disabled="status" v-model="rareSampleModel.tRareSampleEntity.researchsample">
                            <Option value="是">是</Option>
                            <Option value="否">否</Option>
                        </Select>
                    </FormItem>
                </div>
                <div class="title">
                    家系检测成员添加
                    <span class="title-right" @click="familyMemberToggle=!familyMemberToggle" v-show="familyMemberToggle"><Icon type="minus-round"></Icon></span>
                    <span class="title-right" @click="familyMemberToggle=!familyMemberToggle" v-show="!familyMemberToggle"><Icon type="plus-round"></Icon></span>     
                </div>
                <div class="info" v-show="familyMemberToggle">
                    <div v-for="(item, index) in rareSampleModel.tRareamilyEntityList" :key="index">
                        <FormItem label="姓名" :prop="'tRareamilyEntityList.' + index + '.name'"  :rules="{required:true,message:'姓名不能为空',trigger:'blur'}">
                            <Input :disabled="status" v-model="item.name"  placeholder="请输入姓名" />
                        </FormItem>
                        <FormItem label="亲属关系" :prop="'tRareamilyEntityList.' + index + '.relationship'"  :rules="{required:true,message:'亲属关系不能为空',trigger:'change'}">
                            <Select :disabled="status" v-model="item.relationship" placeholder="请选择亲属关系">
                                <Option value="父亲">父亲</Option>
                                <Option value="母亲">母亲</Option>
                                <Option value="兄弟">兄弟</Option>
                                <Option value="姐妹">姐妹</Option>
                                <Option value="其他">其他</Option>
                            </Select>
                        </FormItem>  
                        <FormItem v-show="item.relationship=='其他'">
                            <Input :disabled="status" v-model="item.itsRelationship"  placeholder="请输入其他亲属关系" />
                        </FormItem>                                                
                        <FormItem label="性别" :prop="'tRareamilyEntityList.' + index + '.sex'"  :rules="{required:true,message:'性别不能为空',trigger:'change'}">
                            <Select :disabled="status" v-model="item.sex" placeholder="请选择性别">
                                <Option value="男">男</Option>
                                <Option value="女">女</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="出生日期" :prop="'tRareamilyEntityList.' + index + '.birthday'"  :rules="{required:true,validator:validatevBirthday,trigger:'change'}">
                          <!-- MUI方式 -->      
                          <div class="ivu-input-wrapper ivu-input-type">
                              <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('listBirthday',index)">
                                <span v-if="item.birthday">{{item.birthday}}</span>
                                <span v-else   style="color:#bbbec4;">请选择出生日期</span>
                              </div>
                          </div>  
                        </FormItem>
                        <FormItem label="样本编号" :prop="'tRareamilyEntityList.' + index + '.sampleNum'"  :rules="{required:true,message:'样本编号不能为空',trigger:'blur'}">
                            <Input :disabled="status" v-model="item.sampleNum" placeholder="请输入样本编号" />
                        </FormItem>
                        <FormItem label="是否患病" :prop="'tRareamilyEntityList.' + index + '.isHaveb'"  :rules="{required:true,message:'请选择是否患病',trigger:'change'}">
                          <RadioGroup v-model="item.isHaveb">
                            <Radio :disabled="status" label="否">否</Radio>
                            <Radio :disabled="status" label="是">是</Radio>                            
                          </RadioGroup>
                        </FormItem>

                        <FormItem v-show="item.isHaveb=='是'" label="怀疑疾病" >
                            <Input :disabled="status" v-model="item.susDisease" placeholder="请输入怀疑疾病" />
                        </FormItem>
                        <FormItem v-show="item.isHaveb=='是'" label="首次发病日期" >
                          <!-- MUI方式 -->      
                          <div class="ivu-input-wrapper ivu-input-type">
                              <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('listFirstDate',index)">
                                <span v-if="item.firstDate">{{item.firstDate}}</span>
                                <span v-else   style="color:#bbbec4;">请选择首次发病日期</span>
                              </div>
                          </div>                                  
                        </FormItem> 
                        <FormItem v-show="item.isHaveb=='是'" label="临床关键词">
                            <Input :disabled="status" v-model="item.cliKeyWord" type="textarea"  placeholder="请输入临床关键词" />
                        </FormItem>  
                        <div class="longbtn"> 
                          <Button :disabled="status" type="primary" @click="deleteRelative(index)">删除</Button>   
                        </div>                                  
                        <hr/>
                    </div>   
                    <div class="longbtn">                      
                      <Button  :disabled="status" type="primary" @click="addRelative">点击添加</Button>
                    </div>
                </div>

                <div class="title">
                    阳性位点
                    <span class="title-right" @click="familyToggle=!familyToggle" v-show="familyToggle"><Icon type="minus-round"></Icon></span>
                    <span class="title-right" @click="familyToggle=!familyToggle" v-show="!familyToggle"><Icon type="plus-round"></Icon></span>    
                </div>
                <div class="info" v-show="familyToggle">
                    <div class="note">（当患者亲属不做检测，只做验证时填以下内容）</div>
                    <div v-for="(item, index) in rareSampleModel.rareValidateInfoEntity" :key="index">
                        <FormItem label="姓名" :prop="'rareValidateInfoEntity.' + index + '.vname'"  :rules="{required:true,message:'姓名不能为空',trigger:'blur'}">
                            <Input :disabled="status" v-model="item.vname" placeholder="请输入姓名" />
                        </FormItem>
                        <FormItem label="性别" :prop="'rareValidateInfoEntity.' + index + '.vSex'"  :rules="{required:true,message:'性别不能为空',trigger:'change'}">
                            <Select v-model="item.vSex" :disabled="status" placeholder="请选择性别">
                                <Option value="男">男</Option>
                                <Option value="女">女</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="出生日期" :prop="'rareValidateInfoEntity.' + index + '.vBirthday'"  :rules="{required:true, validator:validatevBirthday,trigger:'change'}">  
                          <!-- MUI方式 -->      
                          <div class="ivu-input-wrapper ivu-input-type">
                              <div v-bind:class="{'div-disabled':status,'ivu-input':true}" @click="confirmDate('vBirthday',index)">
                                <span v-if="item.vBirthday">{{item.vBirthday}}</span>
                                <span v-else   style="color:#bbbec4;">请选择出生日期</span>
                              </div>
                          </div>                               
                        </FormItem>
                        <FormItem label="亲属称谓" :prop="'rareValidateInfoEntity.' + index + '.kinship'"  :rules="{required:true,message:'亲属称谓不能为空',trigger:'blur'}">
                            <Input :disabled="status" v-model="item.kinship" placeholder="请输入亲属称谓"/>
                        </FormItem>
                        <FormItem label="样本编号" :prop="'rareValidateInfoEntity.' + index + '.sampleNum'"  :rules="{required:true,message:'样本编号不能为空',trigger:'blur'}">
                            <Input :disabled="status" v-model="item.sampleNum" placeholder="请输入样本编号" />
                        </FormItem>
                        <FormItem label="是否患病" :prop="'rareValidateInfoEntity.' + index + '.isHaveb'"  :rules="{required:true,message:'请选择是否患病',trigger:'change'}"> 
                            <RadioGroup v-model="item.isHaveb" >
                                <Radio :disabled="status" label="否"></Radio>
                                <Radio :disabled="status" label="是"></Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem v-show="item.isHaveb=='是'">
                            <Input :disabled="status" v-model="item.bName" placeholder="请输入患病名称"  />
                        </FormItem>   
                        <FormItem label="是否单独收费" :prop="'rareValidateInfoEntity.' + index + '.isOrNoCharge'"  :rules="{required:true,message:'请选择是否单独收费',trigger:'change'}"> 
                            <RadioGroup v-model="item.isOrNoCharge" >
                                <Radio :disabled="status" label="否"></Radio>
                                <Radio :disabled="status" label="是"></Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem v-show="item.isOrNoCharge=='是'">
                          <Input :disabled="status" v-model="item.chargeMoney" placeholder="请输入收费金额" />
                        </FormItem>
                        <div class="longbtn">
                          <Button :disabled="status" type="primary" @click="deletePositive(index)">删除亲属</Button>    
                        </div>                     
                        <hr/>
                    </div>
                    <div class="longbtn">
                      <Button :disabled="status" type="primary" @click="addPositive">点击添加亲属</Button>
                    </div>
                </div>
                <div class="title">
                    检测项目
                    <span class="title-right" @click="projectToggle=!projectToggle" v-show="projectToggle"><Icon type="minus-round"></Icon></span>
                    <span class="title-right" @click="projectToggle=!projectToggle" v-show="!projectToggle"><Icon type="plus-round"></Icon></span>     
                </div>
                <div class="info" v-show="projectToggle">
                    <div v-for="(item, index) in rareSampleModel.tRareProjectEntityList" :key="index">
                        <FormItem label="检测项目" :prop="'tRareProjectEntityList.' + index + '.surProject'"  :rules="{required:true,message:'检测项目不能为空',trigger:'change'}">
                            <RadioGroup v-model="item.surProject" vertical>
                                <Radio :disabled="status" label="全外显子检测安捷伦V6"></Radio>
                                <Radio :disabled="status" label="全外显子检测IDT"></Radio>
                                <Radio :disabled="status" label="其它"></Radio>      
                            </RadioGroup>                        
                        </FormItem>
                        <FormItem v-if="item.surProject=='其它'">
                            <Input :disabled="status" v-model="item.itsSurProject" width="100px" placeholder="请输入其他检测项目" />
                        </FormItem>
                    </div>
                </div>
              </Form>
          </div>
          <div id="part2" v-show="!partShow">
              <Form :model="rareSampleModel" ref="rareSampleModelTwo" :rules="ruleFormTwo">
                <div class="title">物流信息</div>
                <div class="info">
                    <FormItem label="快递单号" prop="tRareSampleEntity.logOrderNum">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.logOrderNum"  placeholder="请输入快递单号" />
                    </FormItem>
                        <FormItem label="物流公司">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.logisticsCompany"  placeholder="请输入物流公司" />
                    </FormItem>
                </div>
                <div class="title">价格</div>
                <div class="info">
                    <FormItem label="促销渠道">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.disChannel"  placeholder="请输入促销渠道" />
                    </FormItem>
                    <FormItem  label="经销商" >
                        <Select :disabled="moneyStatus"  v-model="rareSampleModel.tRareSampleEntity.agentid" clearable placeholder="请选择经销商" @on-change="agentChange" >
                            <Option v-for="item in agentList" :value="item.id" :key="item.id" :label="item.agentName "></Option>
                        </Select>
                    </FormItem>   
                    <FormItem  label="医院" >
                        <Select :disabled="moneyStatus"  v-model="rareSampleModel.tRareSampleEntity.hospitalid" clearable placeholder="请选择医院" >
                            <Option v-for="item in hospitalList" :value="item.hospitalId" :key="item.hospitalId" :label="item.hospitalName "></Option>
                        </Select>
                    </FormItem>  
                     <FormItem label="预付款余额" >
                        <Input :disabled="true" v-model="payment" />
                    </FormItem>                       
                    <FormItem label="支付方式" prop="tRareSampleEntity.paymentMethod">
                      <RadioGroup v-model="rareSampleModel.tRareSampleEntity.paymentMethod">
                        <Radio :disabled="moneyStatus" label="预付">预付</Radio>
                        <Radio :disabled="moneyStatus" label="现付">现付</Radio>                            
                      </RadioGroup>
                    </FormItem>
                    <FormItem label="付款方式" v-show="rareSampleModel.tRareSampleEntity.paymentMethod=='现付'">
                        <Select :disabled="status" v-model="rareSampleModel.tRareSampleEntity.payment" placeholder="请选择付款方式">
                            <Option :value="parseInt(1)">微信</Option>
                            <Option :value="parseInt(2)">支付宝</Option>
                            <Option :value="parseInt(3)">POS机</Option>
                            <Option :value="parseInt(4)">转账</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="金额" prop="tRareSampleEntity.monney">
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.monney"  placeholder="请输入金额" number />
                    </FormItem>                    
                    <FormItem label="凭证号" v-show="rareSampleModel.tRareSampleEntity.payment==3">  
                        <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.voucherno" placeholder="请输入凭证号" />
                    </FormItem>
                </div>
                <div class="title">备注</div>
                <div class="info suittextarea">
                    <Input :disabled="status" v-model="rareSampleModel.tRareSampleEntity.remark" type="textarea"   placeholder="特殊情况说明(开票信息等)" />
                </div>

                <div class="title">上传附件
                    <span class="title-right" v-show="imgInfo.length>0">
                      <Button @click="trashVisible=true" v-if="!trashVisible">删除</Button>
                      <Button @click="trashVisible=false" v-else>取消</Button>
                    </span>
                </div>
                <div class="info ">                          
                    <div class="demo-upload-list" v-for="(item,index) in imgInfo" :key="index">
                        <img :src="item.src" @click="handleView(index)">                                 
                        <div class="demo-upload-list-cover" v-show="trashVisible">
                            <Icon type="ios-trash-outline" @click.native="handleRemove(index)"></Icon>
                        </div>
        
                        <Modal v-model="item.visible" >
                          <p slot="header">预览</p>
                          <img :src="item.src" style="width: 100%">
                          <div slot="footer"></div>
                        </Modal>   
                    </div>                          
                    <Upload
                        ref="upload"
                        :show-upload-list="false"
                        :on-success="uploadSuccess" :on-error="uploadError" 
                        :format="['jpg','jpeg','png']"
                        multiple
                        type="drag"
                        :action="uploadUrl"
                        class="demo-upload-wrap">
                        <div class="demo-upload-camera" >
                            <Icon type="ios-camera" ></Icon>
                        </div>
                    </Upload>                        
                  </div>                      
              </Form>
          </div>
        </div>
        
    </div>
</template>

<script>
import topBar from "@/views/component/topbar.vue";
import { sampleData } from "api/index.js";
export default {
  name: "rareInfo",
  data() {
    const validateMonney = (rule, value, callback) => {
      if (!value || value === "") {
        callback(new Error("金额不能为空"));
      } else if (!Number.isInteger(value)) {
        callback(new Error("金额只能输入数字"));
      } else {
        callback();
      }
    };
    const validateDate = (rule, value, callback) => {
      if (!value || value === "") {
        callback(new Error("日期不能为空"));
      } else {
        callback();
      }
    };
    const validateBirthday = (rule, value, callback) => {
      if (!value || value === "") {
        callback(new Error("出生日期不能为空"));
      } else {
        callback();
      }
    };
    const validateAge = (rule, value, callback) => {
      if (!value || value === "") {
        callback(new Error("年龄不能为空"));
      } else if (!Number.isInteger(value)) {
        callback(new Error("年龄只能输入数字"));
      } else {
        callback();
      }
    };
    const validateOrder = (rule, value, callback) => {
      if (!value || value === "") {
        callback(new Error("快递单号不能为空"));
      } else if (value.length<10 ||value.length>14) {
        callback(new Error("快递单号格式不正确"));
      } else {
        callback();
      }
    };
    return {
      spinShow: false,
      imgInfo: [],
      trashVisible: false,
      firstSaveStatus: false,
      formTitle: "单基因遗传病",
      partShow: true,
      status: true,
      personToggle: true,
      familyToggle: true,
      projectToggle: true,
      familyMemberToggle: true,
      moneyStatus:true,
      uploadUrl: "",
      agentList:[],
      hospitalList:[],
      payment:0, //预付款余额
      addressList: [],
      rareSampleModel: {
        tRareSampleEntity: {
          birthday: "",
          allImg: [],
          samplingDate:Mcake.getFormateDate(new Date()),   
          samplingTypeArray: [],
          firstDate: ""
        },
        tRareamilyEntityList: [
          //有一个对象会在页面上默认显示一组成员
        ],
        tRareProjectEntityList: [
          {
            surProject: ""
          }
        ],
        rareValidateInfoEntity: []
      },
      validatevBirthday: validateDate,
      validateAge: validateAge,
      ruleFormOne: {
        "tRareSampleEntity.name": {
          required: true,
          message: "姓名不能为空",
          trigger: "blur"
        },
        "tRareSampleEntity.sex": {
          required: true,
          message: "性别不能为空",
          trigger: "change"
        },
        "tRareSampleEntity.birthday": {
          required: true,
          validator: validateBirthday,
          trigger: "change"
        },
        "tRareSampleEntity.sampleNum": {
          required: true,
          message: "样本编号不能为空",
          trigger: "blur"
        },
        "tRareSampleEntity.address": {
          required: true,
          message: "报告邮寄信息不能为空",
          trigger: "blur"
        },
        "tRareSampleEntity.susDisease": {
          required: true,
          message: "怀疑疾病不能为空",
          trigger: "blur"
        },
        "tRareSampleEntity.cliKeyWord": {
          required: true,
          message: "临床关键词不能为空",
          trigger: "blur"
        },
        "tRareSampleEntity.sampleDate": {
          required: true,
          validator: validateDate,
          trigger: "change"
        },
        "tRareSampleEntity.samplingDate": {
          required: true,
          validator: validateDate,
          trigger: "blur"
        },
        "tRareSampleEntity.firstDate": {
          required: true,
          validator: validateDate,
          trigger: "blur"
        },
        "tRareSampleEntity.ageCancer": {
          required: true,
          validator: validateAge,
          trigger: "blur"
        },
        "tRareSampleEntity.samplingTypeArray": {
          required: true,
          type: "array",
          message: "样本类型不能为空",
          trigger: "change"
        }
      },
      ruleFormTwo: {
        "tRareSampleEntity.logOrderNum": {
          required: true,
          // message: "快递单号不能为空",
          validator:validateOrder,
          trigger: "blur"
        },
        "tRareSampleEntity.monney": {
          required: true,
          validator: validateMonney,
          trigger: "blur"
        },
        "tRareSampleEntity.paymentMethod": {
          required: true,
          message: "支付方式不能为空",
          trigger: "change"
        },
      }
    };
  },
  methods: {
    partToggle(param) {
      this.partShow =param.btnToggle;   
    },
    toggleEditStatus(){
      this.status =false;  
      if(this.$store.state.btnTemp){
         this.moneyStatus=false;
      }else{
        this.moneyStatus=true;
      }    
    },
    clickNextStep(param){
      window.scrollTo(0, 0);
      this.$refs["rareSampleModelOne"].validate(valid => {
        if (valid) {
          this.$refs.topbar.showPartTwo();
        } else {
          this.$Message.error("请把信息填完整");
        }
      });
    },
    saveItem(param){
      let firstSaveStatus=false;
      this.$refs["rareSampleModelOne"].validate(valid => {
        if (valid) {
          this.$refs.topbar.showPartTwo();
          firstSaveStatus=true;
        } else {
          this.$Message.error("请把信息填完整");
        }
      });
      if(firstSaveStatus){    
        let obj= this.standardDate(1);  //status 0暂存 1保存
        this.$refs["rareSampleModelTwo"].validate(valid => {
          if (valid) {   //验证通过            
            this.tempAndSave(obj)
          } else {
            this.$Message.error("请把信息填完整");
          }
        });
      }
    },
    saveTemp() {  
      let obj= this.standardDate(0)  //status 0暂存 1保存
      this.tempAndSave(obj);
    },
    standardDate(status){
      let obj = this.rareSampleModel;
      if (obj.tRareSampleEntity) {
        obj.tRareSampleEntity.status=status;
        if (obj.tRareSampleEntity.birthday == "") {
          obj.tRareSampleEntity.birthday = null;
        } else {
          obj.tRareSampleEntity.birthday = Mcake.getFormateDate(
            obj.tRareSampleEntity.birthday
          );
        }
        if (obj.tRareSampleEntity.firstDate == "") {
          obj.tRareSampleEntity.firstDate = null;
        } else {
          obj.tRareSampleEntity.firstDate = Mcake.getFormateDate(
            obj.tRareSampleEntity.firstDate
          );
        }
      }
      obj.tRareamilyEntityList.forEach(element => {
        if (element.birthday == "") {
          element.birthday = null;
        } else {
          element.birthday = Mcake.getFormateDate(element.birthday);
        }
        if (element.firstDate == "") {
          element.firstDate = null;
        } else {
          element.firstDate = Mcake.getFormateDate(element.firstDate);
        }
      });
      obj.rareValidateInfoEntity.forEach(element => {
        if (element.vBirthday == "") {
          element.vBirthday = null;
        } else {
          element.vBirthday = Mcake.getFormateDate(element.vBirthday);
        }
      });
      return obj;
    },
    tempAndSave(obj){
      let status=obj.tRareSampleEntity.status;
      if (this.$store.state.operation == "add") {
        this.spinShow = true;
        sampleData
          .saveRarePerson(obj)
          .then(res => {
            if (res.code == 0) {
              if(status==0){
                this.$Message.success("暂存成功");
                this.$router.push("/index");
              }
              else{
                this.$Message.success("保存成功");
                this.$router.push("/mySample");
              }
            } else {
              this.$Message.error(res.msg);
            }
          })
          .catch(error => {
            this.$Message.error(error);
          });
      } else {
        this.spinShow = true;
         if(this.$store.state.btnTemp &&status==1){  //从暂存到保存，调新增接口扣金额
          sampleData
          .saveRarePerson(obj)
          .then(res => {
            if (res.code == 0) {
              this.$Message.success("保存成功");
              this.$router.push("/mySample");
            } else {
              this.$Message.error(res.msg);
            }
          })
          .catch(error => {
            this.$Message.error(error);
          });
         }
         else{
          sampleData
            .updateRarePerson(obj)
            .then(res => {
              if (res.code == 0) {
                if(status==0){
                  this.$Message.success("暂存修改成功");
                  this.$router.push("/index");
                }
                else{
                  this.$Message.success("修改成功");
                  this.$router.push("/mySample");
                }
              } else {
                this.$Message.error(res.msg);
              }
            })
            .catch(error => {
              this.$Message.error(error);
            });
         }

      }
    },
    getRareInfo() {
      let obj = {
        id: this.$store.state.sampleid
      };
      sampleData
        .getRarePerson(obj)
        .then(res => {
          if (res.code == 0 || res.code == 200) {
            this.rareSampleModel = res.rareSampleModel;
            let showAgentId=this.rareSampleModel.tRareSampleEntity.agentid;
            if(showAgentId){
              this.getHospitalListByAgentId(showAgentId);
              this.getAgentById(showAgentId);
            }
            if (this.rareSampleModel.tRareProjectEntityList.length == 0) {
              //为了让没有对象的情况下也显示检测内容，其实这种情况正常不存在，因为这项是必填项
              this.rareSampleModel.tRareProjectEntityList.push({});
            }
            let allImg = this.rareSampleModel.tRareSampleEntity.allImg;
            if (allImg) {
              //图片不为空才能使用forEach
              allImg.forEach(element => {
                this.imgInfo.push({ src: element, visible: false });
              });
            }
          } else {
            this.$Message.error(res.msg);
          }
        })
        .catch(error => {
          this.$Message.error(error);
        });
    },
    addRelative() {
      this.rareSampleModel.tRareamilyEntityList.push({
        name: "",
        relationship: "",
        sex: "",
        birthday: "",
        sampleNum: "",
        susDisease: "",
        firstDate: "",
        cliKeyWord: ""
      });
    },
    deleteRelative(index) {
      this.rareSampleModel.tRareamilyEntityList.splice(index, 1);
    },
    addPositive() {
      this.rareSampleModel.rareValidateInfoEntity.push({
        vname: "",
        vBirthday: "",
        sex: "",
        kinship: "",
        sampleNum: "",
        isHaveb: "",
        bName: ""
      });
    },
    deletePositive(index) {
      this.rareSampleModel.rareValidateInfoEntity.splice(index, 1);
    },
    confirmDate(type,index){
      var dtPicker = new mui.DtPicker({
        type: "date",
        beginYear: 1860,
        endDate: new Date()
      });
      dtPicker.show(selectItems => {
        if(type=='birthday'){
          this.rareSampleModel.tRareSampleEntity.birthday = selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "tRareSampleEntity.birthday"
          );
        }
        if(type=='samplingDate'){
          this.rareSampleModel.tRareSampleEntity.samplingDate = selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "tRareSampleEntity.samplingDate"
          );
        }
        if(type=='firstDate'){
          this.rareSampleModel.tRareSampleEntity.firstDate = selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "tRareSampleEntity.firstDate"
          );
        }
        if(type=='listBirthday'){
          this.rareSampleModel.tRareamilyEntityList[index].birthday =
          selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "tRareamilyEntityList." + index + ".birthday"
          );
        }
        if(type=='listFirstDate'){
           this.rareSampleModel.tRareamilyEntityList[index].firstDate =
          selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "tRareamilyEntityList." + index + ".firstDate"
          );
        }
        if(type=='vBirthday'){
          this.rareSampleModel.rareValidateInfoEntity[index].vBirthday =
          selectItems.text;
          this.$refs.rareSampleModelOne.validateField(
            "rareValidateInfoEntity." + index + ".vBirthday"
          );
        }

      });
    },
    uploadSuccess(response, file, fileList) {
      if (response.code == 0 || response.code == 200) {
        if (!this.rareSampleModel.tRareSampleEntity.allImg) {
          //为null需初始化
          this.rareSampleModel.tRareSampleEntity.allImg = new Array();
        }
        this.rareSampleModel.tRareSampleEntity.allImg.push(response.msg);
        this.imgInfo.push({ src: response.msg, visible: false });
      } else {
        this.$Message.error(response.msg);
        this.$refs.upload.clearFiles();
      }
    },
    uploadError(error, file, fileList) {
      this.$Message.error(error.msg);
    },
    handleRemove(index) {
      this.rareSampleModel.tRareSampleEntity.allImg.splice(index, 1);
      this.imgInfo.splice(index, 1);
    },
    handleView(index) {
      this.imgInfo[index].visible = true;
    },
    getAgentListByUserId(){
        let obj={}
        sampleData.getAgentListByUserId(obj).then(res=>{
            if (res.code == 0 || res.code == 200) {
            this.agentList=res.list;
            }       
        }).catch(error=>{
            this.$Message.error(error)
        })
    },
    getHospitalListByAgentId(agentId){
      let obj={
        agentId:agentId
      }
      sampleData.getHospitalListByAgentId(obj).then(res=>{
        if(res.code==0||res.code==200){
          this.hospitalList=res.list;
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error)
      })
    },    
    getAgentById(agentId){
      let obj={
        id:agentId
      }
      sampleData.getAgentById(obj).then(res=>{
        if(res.code==0||res.code==200){
          this.payment=res.agentEntity.payment||0
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error);
      })
    },
    agentChange(agentId){
      this.rareSampleModel.tRareSampleEntity.hospitalid="";
      this.getHospitalListByAgentId(agentId);
      this.getAgentById(agentId);
    }, 
    getAddressMessage(){
      let obj={};
      sampleData.addressMessage(obj).then(res=>{
        if(res.code==0||res.code==200){
          let setList=new Set(res.list); //消除数组中的重复
          this.addressList=[...setList];  //set数据结构转为数组
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error)
      })
    },
    filterAddress(value,option) {
      return option.indexOf(value)!==-1    
    }
  },
  mounted() {
    if (this.$store.state.operation == "edit") {
      this.getRareInfo();
      this.status = true;
      this.moneyStatus=true;
    } else{
      this.status = false;
      this.moneyStatus=false;
    } 

    this.uploadUrl = this.$store.state.uploadUrl;
    this.getAgentListByUserId();
    this.getAddressMessage();
  },
  components: {
    topBar
  }
};
</script>





